﻿#region Copyright

// /*
// SilverlightInEnterprise
// SilverlightInEnterprise.VisualStateManagerDemo
// ChangeStateBehavior.cs
// 
// Author: Matt Van Horn
// Created 11/17/2011 12:32 PM
// Copyright: Silverlight in the Enterprise www.slinenterprise.com
// 
// Licensed under MS-PL http://www.opensource.org/licenses/MS-PL and while you 
// may use this in anyway you see fit and owe us nothing to do so, we wouldn't mind knowing that you got value out of the work we did (so we will do more) so 
// we ask that you leave a comment on www.slinenterprise.com or on the codeplex site slinenterprise.codeplex.com
// 
// */

#endregion

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace SilverlightInEnterprise.VisualStateManagerDemo
{
    public class ChangeStateBehavior : Behavior<Control>
    {
        #region ActiveVisualState 

        public static readonly DependencyProperty ActiveVisualStateProperty =
            DependencyProperty.Register("ActiveVisualState", typeof (string), typeof (ChangeStateBehavior),
                                        new PropertyMetadata(ActiveVisualStateProperty_ChangedCallback));

        public string ActiveVisualState
        {
            get { return (string) GetValue(ActiveVisualStateProperty); }
            set { SetValue(ActiveVisualStateProperty, value); }
        }

        /// <summary>
        ///   DP changed call back for ActiveVisualState
        /// </summary>
        /// <param name = "obj">instance of ChangeStateBehavior that is having its ActiveVisualState property changed</param>
        /// <param name = "e">the DP changed arguments</param>
        private static void ActiveVisualStateProperty_ChangedCallback(DependencyObject obj,
                                                                      DependencyPropertyChangedEventArgs e)
        {
            ChangeStateBehavior element = obj as ChangeStateBehavior;

            element.OnActiveVisualStateChanged(e);
        }

        /// <summary>
        ///   Method will enable classes that derive from ChangeStateBehavior to handle ActiveVisualState changes
        /// </summary>
        /// <param name = "e">the DP changed arguments passed in to the DP changed callback</param>
        protected virtual void OnActiveVisualStateChanged(DependencyPropertyChangedEventArgs e)
        {
            VisualStateManager.GoToState(this.AssociatedObject, this.ActiveVisualState, true);
        }

        #endregion
    }
}